package com.cwx.mychat.aspect;

/**
 * 作者： cwx
 * 时间： 2024/12/3 17:32
 * 版本： 1.0
 * 注释：
 */
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
@Aspect
public class LoggingAspect {
    private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

    @Around("execution(* com.cwx.mychat.aspect.MyService.*(..))") // 指定切点
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("Method " + joinPoint.getSignature().getName() + " is about to be called.");
        Object[] args = joinPoint.getArgs();
        logger.info("获取到参数, {}", args[0]);
        Object result;
        try {
            result = joinPoint.proceed(); // 继续调用目标方法
        } catch (Throwable throwable) {
            logger.error("Method " + joinPoint.getSignature().getName() + " threw an exception: " + throwable);
            throw throwable; // 重新抛出异常
        }

        logger.info("Method " + joinPoint.getSignature().getName() + " completed.");
        return result; // 返回目标方法的结果
    }
}
